<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-form :inline="true">
        <el-form-item label="套餐名称">
          <el-input v-model="listQuery.rechargeName" clearable class="filter-item" placeholder="套餐名称"/>
        </el-form-item>
        <el-button class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
        <el-button
          v-hasPermi="['admin:merchantRechargeActivity:create']"
          class="filter-item"
          style="float:right"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd">添加</el-button>
      </el-form>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row >
      <el-table-column align="center" label="套餐名称" prop="rechargeName" />
      <el-table-column align="center" label="售价金额" prop="realMoney"/>
      <el-table-column align="center" label="充值金额" prop="gainMoney"/>
      <el-table-column align="center" label="创建时间" prop="createTime"/>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['admin:merchantRechargeActivity:update']"
            type="primary"
            size="small"
            @click="handleEdit(scope.row)">编辑</el-button>
          <!-- <el-button
            v-hasPermi="['admin:merchantRechargeActivity:delete']"
            type="primary"
            size="small"
            @click="handleDelete(scope.row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.pageSize" @pagination="getList" />

    <el-dialog :close-on-click-modal="false"
      :visible.sync="dialogVisible"
      :title="dialogType===1?'新增套餐':'修改套餐'"
      width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" class="demo-form">
        <el-form-item label="套餐名称" prop="rechargeName">
          <el-input v-model="form.rechargeName"/>
        </el-form-item>
        <el-form-item label="充值金额" prop="gainMoney">
          <el-input v-model="form.gainMoney" type="Number"/>
        </el-form-item>
        <el-form-item label="售价金额" prop="realMoney">
          <el-input v-model="form.realMoney" type="Number"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList, addRechargeActivity, getRechargeActivityDetail, editRechargeActivity, delRechargeActivity } from './api/index'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20
      },
      dialogVisible: false,
      form: {},
      dialogType: 1,
      rules: {
        rechargeName: [
          { required: true, message: '请输入套餐名称', trigger: 'blur' }
        ],
        realMoney: [
          { required: true, message: '请输入售价金额', trigger: 'blur' }
        ],
        gainMoney: [
          { required: true, message: '请输入实充金额', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.dialogType === 1) {
            addRechargeActivity(this.form).then(res => {
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '新增成功!'
              })
              this.getList()
            })
          } else {
            editRechargeActivity(this.form).then(res => {
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '编辑成功!'
              })
              this.getList()
            })
          }
        }
      })
    },
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        let data = response.data || {}
        this.list = data.list || []
        this.total = Number(data.total || 0)
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleAdd() {
      this.dialogType = 1
      this.dialogVisible = true
      this.form = {}
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
    },
    handleEdit(row) {
      this.dialogType = 2
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.form.clearValidate()
      })
      getRechargeActivityDetail(row.id).then(res => {
        this.form = res.data || {}
      })
    },
    handleDelete(row) {
      this.$confirm(`确定删除套餐${row.rechargeName}吗？`, '温馨提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRechargeActivity(row.id).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>
<style scoped lang="scss">
.admin-list{
  border: 1px solid #BEBDBD;
  box-shadow: 0px 2px 3px 0px rgba(64,158,255,0.1608);
  border-radius: 1px 1px 1px 1px;
  padding: 5px;
  height: 80px;
}
.el-tag + .el-tag {
    margin-left: 10px;
  }
</style>
